package com.example.task1.ui.AllService.traffic;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.TrafficTheBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class TrafficTheListAdapter extends BaseAdapter<TrafficTheBean.DataDTO> {

    private Context context;

    private List<TrafficTheBean.DataDTO> mData = new ArrayList<>();
    private TextView tvApplyDate;
    private TextView tvAuditOffice;
    private TextView tvContact;
    private TextView tvFileNo;
    private TextView tvIdCard;
    private TextView tvLicenseLevel;
    private TextView tvLicenseNo;
    private TextView tvTimeout;
    private TextView tvVerifyDate;
    private Button btnRemove;

    public TrafficTheListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<TrafficTheBean.DataDTO> rows) {
        mData.clear();
        mData.addAll(rows);
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.trafficthlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<TrafficTheBean.DataDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, TrafficTheBean.DataDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, TrafficTheBean.DataDTO date, int position) {
        tvApplyDate.setText("申领日期：" + date.getApplyDate());
        tvAuditOffice.setText("发证机关：" + date.getAuditOffice());
        tvContact.setText("联系方式：" + date.getContact());
        tvFileNo.setText("档案编号：" + date.getFileNo());
        tvIdCard.setText("身份证号：" + date.getIdCard());
        tvLicenseLevel.setText("准驾车型：" + date.getLicenseLevel());
        tvLicenseNo.setText("驾驶证号：" + date.getLicenseNo());
        tvTimeout.setText("否".equals(date.getApplyDate()) ? "有效" : "已过期");
        tvVerifyDate.setText("有效期：" + date.getVerifyDate());
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> del = Api.api().del(MyApplication.TOKEN, "/prod-api/api/traffic/license/" + date.getId());
                Api.send(del, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("解绑成功");
                        mData.remove(position);
                        notifyItemRemoved(position);
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View view) {
        tvApplyDate = (TextView) view.findViewById(R.id.tv_applyDate);
        tvAuditOffice = (TextView) view.findViewById(R.id.tv_auditOffice);
        tvContact = (TextView) view.findViewById(R.id.tv_contact);
        tvFileNo = (TextView) view.findViewById(R.id.tv_fileNo);
        tvIdCard = (TextView) view.findViewById(R.id.tv_idCard);
        tvLicenseLevel = (TextView) view.findViewById(R.id.tv_licenseLevel);
        tvLicenseNo = (TextView) view.findViewById(R.id.tv_licenseNo);
        tvTimeout = (TextView) view.findViewById(R.id.tv_timeout);
        tvVerifyDate = (TextView) view.findViewById(R.id.tv_verifyDate);
        btnRemove = (Button) view.findViewById(R.id.btn_remove);
    }
}
